package com.boc.ewr.work.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.work.entity.vo.WorkRecordDtlListVo;
import com.boc.ewr.work.entity.vo.WorkRecordVo;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.boc.ewr.work.entity.WorkRecordDtl;
import com.boc.ewr.work.service.IWorkRecordDtlService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 工作记录明细
 * @Author: jeecg-boot
 * @Date: 2023-05-17
 * @Version: V1.0
 */
@Api(tags = "工作记录明细")
@RestController
@RequestMapping("/work/workRecordDtl")
@Slf4j
public class WorkRecordDtlController extends JeecgController<WorkRecordDtl, IWorkRecordDtlService> {
    @Autowired
    private IWorkRecordDtlService workRecordDtlService;

    /**
     * 分页列表查询
     *
     * @param workRecordDtl
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "工作记录明细-分页列表查询")
    @ApiOperation(value = "工作记录明细-分页列表查询", notes = "工作记录明细-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<WorkRecordDtl>> queryPageList(WorkRecordDtl workRecordDtl,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<WorkRecordDtl> queryWrapper = QueryGenerator.initQueryWrapper(workRecordDtl, req.getParameterMap());
        Page<WorkRecordDtl> page = new Page<WorkRecordDtl>(pageNo, pageSize);
        IPage<WorkRecordDtl> pageList = workRecordDtlService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param workRecordDtl
     * @return
     */
    @AutoLog(value = "工作记录明细-添加")
    @ApiOperation(value = "工作记录明细-添加", notes = "工作记录明细-添加")
    //@RequiresPermissions("com.boc.ewr:work_record_dtl:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody WorkRecordDtl workRecordDtl) {
        workRecordDtlService.save(workRecordDtl);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param workRecordDtl
     * @return
     */
    @AutoLog(value = "工作记录明细-编辑")
    @ApiOperation(value = "工作记录明细-编辑", notes = "工作记录明细-编辑")
    //@RequiresPermissions("com.boc.ewr:work_record_dtl:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody WorkRecordDtl workRecordDtl) {
        workRecordDtlService.updateById(workRecordDtl);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "工作记录明细-通过id删除")
    @ApiOperation(value = "工作记录明细-通过id删除", notes = "工作记录明细-通过id删除")
    //@RequiresPermissions("com.boc.ewr:work_record_dtl:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        workRecordDtlService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "工作记录明细-批量删除")
    @ApiOperation(value = "工作记录明细-批量删除", notes = "工作记录明细-批量删除")
    //@RequiresPermissions("com.boc.ewr:work_record_dtl:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.workRecordDtlService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "工作记录明细-通过id查询")
    @ApiOperation(value = "工作记录明细-通过id查询", notes = "工作记录明细-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WorkRecordDtl> queryById(@RequestParam(name = "id", required = true) String id) {
        WorkRecordDtl workRecordDtl = workRecordDtlService.getById(id);
        if (workRecordDtl == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(workRecordDtl);
    }

    /**
     * 通过cusmNm查询
     *
     * @param cusmNm
     * @return
     */
    //@AutoLog(value = "工作记录明细-通过cusmNm查询")
    @ApiOperation(value = "工作记录明细-通过cusmNm查询", notes = "工作记录明细-通过cusmNm查询")
    @GetMapping(value = "/queryByCusmNm")
    public Result<WorkRecordVo> queryByCusmNm(@RequestParam(name = "cusmNm", required = true) String cusmNm) {
        List<WorkRecordDtl> workRecordDtlList = workRecordDtlService.list(
                new LambdaQueryWrapper<WorkRecordDtl>()
                        .eq(WorkRecordDtl::getCusmNm, cusmNm)
                        .orderByDesc(WorkRecordDtl::getCommunicateTime)
        );
        if (workRecordDtlList.size() == 0) {
            return Result.error("未找到对应数据");
        } else {
            WorkRecordVo workRecordVo = new WorkRecordVo();
            BeanUtils.copyProperties(workRecordDtlList.get(0), workRecordVo);
            workRecordVo.setDtlListVoList(new ArrayList<>());
            // 只展示最近的三次记录
            int count = 0;
            for (WorkRecordDtl workRecordDtl : workRecordDtlList) {
                if (count < 3) {
                    WorkRecordDtlListVo workRecordDtlListVo = new WorkRecordDtlListVo();
                    BeanUtils.copyProperties(workRecordDtl, workRecordDtlListVo);
                    workRecordVo.getDtlListVoList().add(workRecordDtlListVo);
                    count++;
                } else {
                    break;
                }
            }
            return Result.OK(workRecordVo);
        }
    }

    /**
     * 导出excel
     *
     * @param request
     * @param workRecordDtl
     */
    //@RequiresPermissions("com.boc.ewr:work_record_dtl:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WorkRecordDtl workRecordDtl) {
        return super.exportXls(request, workRecordDtl, WorkRecordDtl.class, "工作记录明细");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("work_record_dtl:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WorkRecordDtl.class);
    }

}
